package com.letwal.cmcvnd.components.dialog.selectAsset

import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.view.WindowManager
import android.widget.EditText
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.letwal.cmcvnd.R
import com.letwal.cmcvnd.adata.coin.RevDataGetCoins
import com.letwal.cmcvnd.components.dialog.selectAsset.adapter.SelectAssetAdapter


class SelectAssetDialog(context: Context, theme: Int = R.style.BottomSheetDialog) :
    BottomSheetDialog(context, theme) {

    private var rootView: View = layoutInflater.inflate(R.layout.dialog_select_asset, null)
    private lateinit var listBarDialogSelectAsset: RecyclerView
    private lateinit var editBarDialogSelectAsset: EditText
    private lateinit var textDialogSelectAssetCurrent: TextView

    private lateinit var mSelectAssetAdapter: SelectAssetAdapter
    private val mDataList: MutableList<RevDataGetCoins> = mutableListOf()
    private val mDataListTemp: MutableList<RevDataGetCoins> = mutableListOf()
    private var mTitle: String? = null

    //点击事件
    private var mItemClickListener: (RevDataGetCoins) -> Unit = {}

    //输入事件
    private var mItemInputListener: (String) -> Unit = {}

    constructor(context: Context) : this(context, R.style.BottomSheetDialog) {
        setContentView(rootView)

//        setCancelable(false)
//        setCanceledOnTouchOutside(false)

        // 设置对话框窗口属性以实现全屏显示
        val layoutParams = window?.attributes
        layoutParams?.width = WindowManager.LayoutParams.MATCH_PARENT
        layoutParams?.height = WindowManager.LayoutParams.MATCH_PARENT
        window?.attributes = layoutParams
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        listBarDialogSelectAsset = findViewById(R.id.listBarDialogSelectAsset)!!
        editBarDialogSelectAsset = findViewById(R.id.editBarDialogSelectAsset)!!
        textDialogSelectAssetCurrent = findViewById(R.id.textDialogSelectAssetCurrent)!!

        //初始化适配器
        mSelectAssetAdapter = SelectAssetAdapter(mDataList)
        mSelectAssetAdapter.setItemClickListener(mItemClickListener)
        listBarDialogSelectAsset.layoutManager = LinearLayoutManager(context)
        listBarDialogSelectAsset.adapter = mSelectAssetAdapter

        //初始化edit配置
        editBarDialogSelectAsset.addTextChangedListener(object :
            TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun afterTextChanged(p0: Editable?) {
                //更新列表
                filterDataList()
                //mItemInputListener.invoke(editBarDialogSelectAsset.text.toString().trim())
            }
        })
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onStart() {
        super.onStart()
        // 获取 BottomSheetBehavior
        val bottomSheetBehavior = behavior
        bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED

        //更新title
        textDialogSelectAssetCurrent.text = mTitle

        //更新列表
        mSelectAssetAdapter.notifyDataSetChanged()
    }

    /** 更新数据源 */
    fun updateDataList(list: List<RevDataGetCoins>) {
        mDataList.clear()
        mDataList.addAll(list)

        mDataListTemp.clear()
        mDataListTemp.addAll(list)
    }

    /** 过滤数据源 */
    @SuppressLint("NotifyDataSetChanged")
    fun filterDataList() {
        val content = editBarDialogSelectAsset.text.toString().trim()
        mDataList.clear()
        mDataListTemp.forEach { item ->
            if (item.coinName?.contains(content, true) == true) {
                mDataList.add(item)
            } else if (item.name?.contains(content, true) == true) {
                mDataList.add(item)
            }
        }
        mSelectAssetAdapter.notifyDataSetChanged()
    }

    /** 更新数据源 */
    fun updateCurrentTitle(title: String?) {
        mTitle = title
    }

    /** 设置点击事件 */
    fun setItemClickListener(listener: (RevDataGetCoins) -> Unit) {
        mItemClickListener = listener
    }

    /** 设置输入事件 */
    fun setItemInputListener(listener: (String) -> Unit) {
        mItemInputListener = listener
    }
}